//
//  XTVCOfpaihang.m
//  TTLestening
//
//  Created by dllo on 16/3/24.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "XTVCOfpaihang.h"
#import "NetworkingHandlerOfBlock.h"
#import "XTModelOfpaibang.h"
#import "XTCellOfpaihang.h"
#import "XTWebview.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "XCAVMusicPlayer.h"


#define WIDTH  [UIScreen mainScreen].bounds.size.width
#define HEIGHT  [UIScreen mainScreen].bounds.size.height
@interface XTVCOfpaihang ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain)UITableView *tableview;
@property (nonatomic,retain)NSMutableArray *array;

@property (nonatomic,retain)NSMutableDictionary *dic;

@property (nonatomic,retain)NSArray *arratsection;
@end

@implementation XTVCOfpaihang

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor redColor];
    self.title = @"排行榜";
    [self creatview];
    [self creatData];
    
}

-(void)creatview{
    self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,WIDTH, HEIGHT - 49) style:UITableViewStylePlain];
    self.tableview.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableview];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    UINib *nib = [UINib nibWithNibName:@"XTCellOfpaihang" bundle:[NSBundle mainBundle]];
    
    [self.tableview registerNib:nib forCellReuseIdentifier:@"pool1"];

    
}

-(void)creatData{
    
    NSString *url = @"http://api.songlist.ttpod.com/channels/bhb/children?app=ttpod&v=v8.1.5.2016022418&user_id=0&mid=iPhone5C&f=f320&s=s310&imsi=&hid=&splus=9.2&active=1&net=2&openudid=71ae591d23e3955faffe1f13002174549b820ffe&idfa=766C509A-D4C4-4D11-89EA-A316DCF2B7CC&utdid=Vo%2FO9IPtv1UDAHSbJQCXwF%2F6&alf=201200&bundle_id=com.ttpod.music&latitude=38.88253&longtitude=121.5397";
    
    [NetworkingHandler networkingWithUrl:url completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        self.arratsection = [NSArray array];
        self.dic = [NSMutableDictionary dictionary];
       
       
                for (NSDictionary *dic in result) {
            
                    NSString *key = [dic objectForKey:@"name"];
                
                    
                    NSArray *arr = [dic objectForKey:@"refs"];
                    NSMutableArray *arrqqqqq = [NSMutableArray array];

                    for (NSDictionary * dicapp in arr) {
                        
                        XTModelOfpaibang *model = [[XTModelOfpaibang alloc]init];
                        model.arr = [NSMutableArray array];
                        [model setValuesForKeysWithDictionary:dicapp];
                        
                        model.link = [dicapp objectForKey:@"link"];
                        model.image = [[dicapp objectForKey:@"image"]objectForKey:@"pic"];
                       
                        
                        for (NSDictionary *dicstr in [dicapp objectForKey:@"songs"]) {
                            NSString *str = [dicstr objectForKey:@"name"];
                            NSString *str2 = [dicstr objectForKey:@"singerName"];
                            
                            NSString *Str3 = [NSString stringWithFormat:@"%@-%@",str,str2];

                            [model.arr addObject:Str3];
                            
                        }
                         [arrqqqqq addObject:model];
                         [self.dic setObject:arrqqqqq forKey:key];
                }
                   
                    self.arratsection = [self.dic allKeys];

                }
        [self.tableview reloadData];
    }];
    
    

    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return WIDTH  *0.31;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  NSString *str =  [self.arratsection objectAtIndex:section];
   
  NSArray *arr =   [self.dic objectForKey:str];

   return  arr.count;
    
   }

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *str =  [self.arratsection objectAtIndex:indexPath.section];
    NSArray *arr =   [self.dic objectForKey:str];
    
    XTCellOfpaihang *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
    XTModelOfpaibang *model = [[XTModelOfpaibang alloc]init];
    
    
    model = [arr objectAtIndex:indexPath.row];

        [cell.image sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:nil options:SDWebImageAvoidAutoSetImage];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.title.text = model.title;
        
        cell.geming1.text = [model.arr objectAtIndex:0];
        cell.geming2.text = [model.arr objectAtIndex:1];
        cell.geming3.text = [model.arr objectAtIndex:2];
  
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.arratsection.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    NSString *key = [self.arratsection objectAtIndex:section];
    
    
    
    return key;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 40;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *str =  [self.arratsection objectAtIndex:indexPath.section];
    NSArray *arr =   [self.dic objectForKey:str];
    XTModelOfpaibang *model = [[XTModelOfpaibang alloc]init];
    
    
    model = [arr objectAtIndex:indexPath.row];
    
    if (![model.link isEqual:[NSNull null]]) {
        XTWebview *vc = [[XTWebview alloc]init];
        vc.urlstring = model.link;
        
        [self.navigationController pushViewController:vc animated:YES];
    }else{
    
        NSLog(@"%@",model.songlist_id);
        
        [[NSNotificationCenter defaultCenter]postNotificationName:@"panghangbang" object:nil userInfo:@{@"songlistid":model.songlist_id}];
        
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
